﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;

namespace OptimizationDemo.Optimization
{
    public static class MvcHelper
    {
        public static readonly string ScriptAsset = "script";
        public static readonly string StyleAsset = "style";

        /// <summary>
        /// Use this helper on sub layouts to add reference to bundled style sheets
        /// </summary>
        /// <param name="path"></param>
        /// <param name="assetType"></param>
        public static void AddReference(string path, string assetType)
        {
            if (IsOptimizationEnabled)
            {
                var key = string.Concat("ref_", assetType, path);
                if (HttpContext.Current.Items.Contains(key))
                    HttpContext.Current.Items.Remove(key);
                HttpContext.Current.Items.Add(key, path);
            }
        }

        /// <summary>
        /// Use this helper on master page to print bundled stylesheets
        /// </summary>
        /// <returns></returns>
        public static MvcHtmlString RenderStyles()
        {
            if (IsOptimizationEnabled)
            {
                var styles = string.Empty;
                var keysInContext = new List<object>(HttpContext.Current.Items.Keys.Cast<object>());
                foreach (var key in keysInContext)
                {
                    var tempkey = key as string;
                    if (!string.IsNullOrWhiteSpace(tempkey) && tempkey.StartsWith("ref_style"))
                    {
                        var value = HttpContext.Current.Items[tempkey] as string;
                        if (!string.IsNullOrWhiteSpace(value))
                        {
                            var url = value; //.Replace("~", "~__/");
                            styles += Styles.Render(url);
                        }
                    }
                }
                return new MvcHtmlString(styles);
            }
            return new MvcHtmlString(string.Empty);
        }

        /// <summary>
        /// Use this helper on master page to print bundled scripts
        /// </summary>
        /// <returns></returns>
        public static MvcHtmlString RenderScripts()
        {
            if (IsOptimizationEnabled)
            {
                var scripts = string.Empty;
                var keysInContext = new List<object>(HttpContext.Current.Items.Keys.Cast<object>());
                foreach (var key in keysInContext)
                {
                    var tempkey = key as string;
                    if (!string.IsNullOrWhiteSpace(tempkey) && tempkey.StartsWith("ref_script"))
                    {
                        var value = HttpContext.Current.Items[tempkey] as string;
                        if (!string.IsNullOrWhiteSpace(value))
                        {
                            var url = value; //.Replace("~", "~__/");
                            scripts += Scripts.Render(url);
                        }
                    }
                }
                return new MvcHtmlString(scripts);
            }
            return new MvcHtmlString(string.Empty);
        }

        private static bool IsOptimizationEnabled
        {
            get { return true; }
        }
    }
}